1 using UnityEngine;
2 using
System.Collections;
3
4 ///
<summary>
5 ///
Implements OnClick to destroy the GameObject it's attached to. Optionally a RPC is sent to do this.
6 ///
</summary>
7 ///
<remarks>
8 ///
Using an RPC to Destroy a GameObject allows any player to Destroy a GameObject. But it might cause errors.
9 ///
RPC and the Instantiated GameObject are not fully linked on the server. One might stick in the server witout
10 ///
the other.
11 ///

12 ///
A buffered RPC gets cleaned up when the sending player leaves the room. This means, the RPC gets lost.
13 ///

14 ///
Vice versus, a GameObject Instantiate might get cleaned up when the creating player leaves a room.
15 ///
This way, the GameObject that a RPC targets might become lost.
16 ///

17 ///
It makes sense to test those cases. Many are not breaking errors and you just have to be aware of them.
18 ///

19 ///
Gets OnClick() calls by InputToEvent class attached to a camera.
20 ///
</remarks>
21 [RequireComponent(
typeof(PhotonView))]
22 public
class OnClickDestroy : Photon.MonoBehaviour
23 {
24     
public bool DestroyByRpc;
25
26     
public void OnClick()
27     {
28         
if (!DestroyByRpc)
29         {
30             PhotonNetwork.Destroy(
this.gameObject);
31         }
32         
else
33         {
34             
this.photonView.RPC("DestroyRpc", PhotonTargets.AllBuffered);
35         }
36     }
37
38     
[RPC]
39     
public IEnumerator DestroyRpc()
40     {
41         GameObject.Destroy(
this.gameObject);
42         
yield return 0; // if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.
43         PhotonNetwork.UnAllocateViewID(
this.photonView.viewID);
44     }
45 }


Implements OnClick to destroy the GameObject it's attached to. Optionally a RPC is sent to do this.

Using an RPC to Destroy a GameObject allows any player to Destroy a GameObject. But it might cause errors.

RPC and the Instantiated GameObject are not fully linked on the server. One might stick in the server witout

the other.

A buffered RPC gets cleaned up when the sending player leaves the room. This means, the RPC gets lost.

Vice versus, a GameObject Instantiate might get cleaned up when the creating player leaves a room.

This way, the GameObject that a RPC targets might become lost.

It makes sense to test those cases. Many are not breaking errors and you just have to be aware of them.

Gets OnClick() calls by InputToEvent class attached to a camera.

yield return 0; if you allow 1 frame to pass, the object's OnDestroy() method gets called and cleans up references.




Trò chơi Tic-Tac-Toe, game đánh caro full source code 53.480 lượt xem

Gõ tìm kiếm nhanh...